package com.xunqi.blog.web.admin;

import com.xunqi.blog.pojo.Tag;
import com.xunqi.blog.service.TagService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin")
public class TagController {

    @Resource
    private TagService tagService;

    @GetMapping(value = "/tags")
    public String tags(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
                       Pageable pageable, Model model) {

        Page<Tag> page = tagService.listTag(pageable);
        model.addAttribute("page",page);
        return "admin/tags";
    }

    @GetMapping(value = "/tags/input")
    public String addTags(Model model) {
        //跳转到新增页面去传一个tag值过去，避免前端报错
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }


    @PostMapping(value = "/tagssave")
    public String tagAddSave(@Valid Tag tag, BindingResult result, RedirectAttributes attributes) {
        //如果新增过来的name值与数据库原有的name值相同
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1 != null) {
            result.rejectValue("name","nameError","不能添加重复的标签");
        }
        //如果result有错误，返回到新增页面
        if (result.hasErrors()) {
            return "admin/tags-input";
        }

        //执行新增操作
        Tag t = tagService.saveTag(tag);
        if (t == null) {
            attributes.addFlashAttribute("message","添加失败");
        } else {
            attributes.addFlashAttribute("message","添加成功");
        }
        return "redirect:/admin/tags";
    }

    @GetMapping(value = "/tags/{id}/input")
    public String editInput(@PathVariable Long id,Model model) {
        Tag tag = tagService.getTagById(id);
        model.addAttribute("tag",tag);
        return "admin/tags-input";
    }


    @PostMapping(value = "/tagssave/{id}")
    public String tagUpdateSave(@Valid Tag tag, BindingResult result,@PathVariable Long id, RedirectAttributes attributes) {
        //如果新增过来的name值与数据库原有的name值相同
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1 != null) {
            result.rejectValue("name","nameError","不能添加重复的标签");
        }
        //如果result有错误，返回到新增页面
        if (result.hasErrors()) {
            return "admin/tags-input";
        }

        //执行新增操作
        Tag t = tagService.updateTag(id,tag);
        if (t == null) {
            attributes.addFlashAttribute("message","更新失败");
        } else {
            attributes.addFlashAttribute("message","更新成功");
        }
        return "redirect:/admin/tags";
    }


    @GetMapping(value = "/tags/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes) {
        tagService.deleteTag(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/tags";
    }
}
